"""
Author        yongfa
Date          2022-07-11 15:41:45
LastEditTime  2023-09-05 08:08:24
LastEditors   yongfa
Description   功能描述
"""

import os
import time
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore

rootdir = os.path.join(os.path.dirname(__file__), '..')
basedir = os.path.abspath(rootdir)


class BaseConfig:
    "基础配置"
    DEBUG = False
    TESTING = False
    JSON_AS_ASCII = False  # 返回体中文乱码
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SECRET_KEY = os.getenv('SECRET_KEY', 'very cv_parser_api secret')
    ENCRYPT_KEY = os.getenv('ENCRYPT_KEY', 'CF3tEA1J2hRyIOw6')

    PROJECT_NAME = 'cv_parser_api'

    JSONIFY_MIMETYPE = 'application/json; charset=utf-8'

    # default_delete_time 软删除和唯一索引冲突，null对索引不生效，所以默认delete_time
    DEFAULT_DELETE_TIME = '1970-01-01 00:00:00'
    # 静态文件
    STATIC_FOLDER = os.path.join(basedir, 'app', 'static')
    TEMPLATES_FOLDER = os.path.join(basedir, 'app', 'templates')

    # 公共的上传目录
    PUBLIC_UPLOAD_RELATIVE_FOLDER = os.path.join('static', 'uploads/')
    PUBLIC_UPLOAD_FOLDER = os.path.join(basedir, 'app', PUBLIC_UPLOAD_RELATIVE_FOLDER)
    # 图片上传路径
    UPLOAD_FOLDER = os.getenv('UPLOAD_FOLDER', PUBLIC_UPLOAD_FOLDER)
    # 图片格式和大小控制
    ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'ico',
                          'pdf', 'docx', 'doc', 'xlsx', 'xls', 'csv'}
    # 16MB
    MAX_CONTENT_LENGTH = 16 * 1024 * 1024

    # 静态文件地址
    STATIC_LOCAL_URL = os.getenv('STATIC_LOCAL_URL', '')

    # 数据库
    SQLALCHEMY_DATABASE_URI = os.environ.get(
        'SQLALCHEMY_DATABASE_URI', "mysql+pymysql://root:root321@127.0.0.1:3306/restx")

    SQLALCHEMY_POOL_RECYCLE = 8
    # 查询效率阀值 秒
    FLASKY_SLOW_DB_QUERY_TIME = 0.05
    SQLALCHEMY_RECORD_QUERIES = True

    # redis
    REDIS_HOST = os.getenv('REDIS_HOST', '172.19.43.180')
    REDIS_PORT = os.getenv('REDIS_PORT', 6379)
    REDIS_PASSWORD = os.getenv('REDIS_PASSWORD', '123456')
    REDIS_DB = os.getenv('REDIS_DB', 10)

    # flask-redis
    REDIS_URL = "redis://:{password}@{host}:{port}/{db}".format(
        password=REDIS_PASSWORD, host=REDIS_HOST, port=REDIS_PORT, db='8')

    # Flask-Caching | redis
    CACHE_TYPE = 'redis'
    CACHE_REDIS_HOST = REDIS_HOST
    CACHE_REDIS_PORT = REDIS_PORT
    CACHE_REDIS_PASSWORD = REDIS_PASSWORD
    CACHE_REDIS_DB = REDIS_DB

    OAUTH2_REFRESH_TOKEN_GENERATOR = True  # 开启刷新token功能

    # JWT
    JWT_SECRET_KEY = os.getenv('JWT_SECRET_KEY', 'qwfk9vcu7zj4alps')
    JWT_TOKEN_LOCATION = 'headers'
    JWT_HASH = 'HS256'
    JWT_HEADER_NAME = 'Authorization'
    JWT_TOKEN_EXPIRES = int(os.getenv('JWT_TOKEN_EXPIRES', 604800))  # token 过期时间 单位秒
    JWT_ACCESS_TOKEN_EXPIRES = JWT_TOKEN_EXPIRES  # token 过期时间 单位秒
    JWT_REFRESH_TOKEN_EXPIRES = JWT_TOKEN_EXPIRES * 7  # refresh token 过期时间 单位秒

    # FLASK_LIMTER
    RATELIMIT_DEFAULT = os.getenv('RATELIMIT_DEFAULT', '2000/hour;24000/day')

    # 若doc_path=False，则不显示swagger文档
    DOCS_PATH = os.getenv('DOCS_PATH', '/docs')

    # sentry
    SENTRY_URL = os.getenv(
        'SENTRY_URL', 'https://fa5eeaafcd9c42e198faf9553b64eac1@o371039.ingest.sentry.io/4505598999855104')

    # 保护的表名
    PROTECT_TABLES = ['apscheduler_jobs', 'casbin_rule']

    # 邮箱任务限制
    RESUME_EMAIL_TASK_LIMIT = bool(int(os.getenv("RESUME_EMAIL_TASK_LIMIT", 0)))
    RESUME_EMAIL_TASK_LIMIT_MINS = int(os.getenv("RESUME_EMAIL_TASK_LIMIT_MINS", 3))

    # celery 调试模式
    CELERY_DEBUG_MODEL = bool(int(os.getenv("CELERY_DEBUG_MODEL", 0)))

    ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')
    API_BASE_URL = os.getenv('API_BASE_URL', 'http://localhost:5000/api/v1')

class DevelopmentConfig(BaseConfig):
    "开发环境配置"
    DEBUG = True
    ENV = 'dev'
    SQLALCHEMY_ECHO = True


class ProductionConfig(BaseConfig):
    """生产环境配置"""
    DEBUG = False
    ENV = 'prod'
    SWAGGER_URL = False


class TestingConfig(BaseConfig):
    "测试环境配置"
    DEBUG = True
    TESTING = True
    ENV = 'test'


configs = {
    'dev': DevelopmentConfig,
    'prod': ProductionConfig,
    'test': TestingConfig,
    'default': DevelopmentConfig
}
